// 导入路由与vue
import Vue from "vue";
import VueRouter from "vue-router";
// 导入登入组件
import Login from "@/views/Login.vue";
// Home组件
import Home from '@/views/Home'
// Home子组件 Welcome
import Welcome from "@/views/Welcome";
// 用户列表组件
import Users from "@/views/user/Users";
import Rights from "@/views/power/Rights";
import Roles from "@/views/power/Roles"
import Cate from "@/views/goods/Cate"
import Params from "@/views/goods/Params"
import List from "@/views/goods/List"
import Add from "@/views/goods/Add"
import Order from "@/views/order/Order"
import Report from "@/views/report/Report"

// 路由是插件 所有use
Vue.use(VueRouter)
// 暴露路由
const router = new VueRouter({
    routes: [
        // 路由重定向
        {
            path: "/",
            redirect: "/login"
        },
        {
            path: "/login",
            component: Login
        },
        {
            path: "/home",
            component: Home,
            // 路由重定向
            redirect:"/welcome",
            children:[
                {
                    path:"/welcome",
                    component:Welcome
                },
                // 用户组件
                {
                    path:"/users",
                    component:Users
                },
                // 权限列表
                {
                    path:"/rights",
                    component:Rights
                },
                // 角色列表
                {
                    path:"/roles",
                    component:Roles
                },
                // 商品分类
                {
                    path:"/categories",
                    component:Cate
                },
                {
                    path:"/params",
                    component:Params
                },
                {
                    path:"/goods",
                    component:List
                },
                {
                    path:"/goods/add",
                    component:Add
                },
                {
                    path:"/orders",
                    component:Order
                },
                {
                    path:"/reports",
                    component:Report
                }
            ]
        }
    ]
})
// 全局前置路由守卫
router.beforeEach((to, from, next) => {
    // 如果访问的是login直接放行
    if (to.path == "/login") return next()
    // 获取token
    const tokenStr = sessionStorage.getItem("token")
    // 如果没有值，强制跳到login界面
    if(!tokenStr) return next("/login")
    // 都没有return出去，说明可以放行
    next()
})
// 暴露router
export default router